const app = getApp()
const WXAPI = require('apifm-wxapi')
const AUTH = require('../../../utils/auth')

Page({
  data: {
    wxlogin: true,

    aboutUsTitle: '',
    aboutUsContent: '',
    servicePhoneNumber: '',
    balance: 0,
    freeze: 0,
    score: 0,
    score_sign_continuous: 0,
    iconSize: 45,
    iconColor: '#999999'
  },
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading()
    that.onShow()
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onLoad() {
    this.setData({
      version: app.globalData.version,
      background_color: app.globalData.globalBGColor,
      bgRed: app.globalData.bgRed,
      bgGreen: app.globalData.bgGreen,
      bgBlue: app.globalData.bgBlue,
      logo: wx.getStorageSync('logo')
    })
  },
  onShow() {
    var that = this;
    
    AUTH.checkHasLogined().then(isLogined => {
      console.log("确认用户是否登陆："+isLogined)
      that.setData({
        wxlogin: isLogined
      })
      if (isLogined) {
        // 获取用户信息
        console.log("获取用户信息")
        that.getUserApiInfo();
        that.getUserAmount();
        that.checkScoreSign();
      }
    })
    
  },

  //选择身份
  async selectUserType(){
    var that = this;
    wx.showModal({
      title: '我要合作',
      content: '请选择合作方式',
      confirmText: '我是果农',
      cancelText: '我是果商',
      success: function (res) {
        var selectTypeRes;
        if (res.confirm) {
          selectTypeRes = WXAPI.userSelectType({'userId':wx.getStorageSync('userid'),'type':9})
        } else if (res.cancel) {
          selectTypeRes = WXAPI.userSelectType({'userId':wx.getStorageSync('userid'),'type':8})         
        }
        console.log("selectTypeRes:"+JSON.stringify(selectTypeRes));
        
        wx.showModal({
          title: '我要合作',
          content: '合作成功！',
          showCancel: false,
        })
       }
       
      })
      that.getUserApiInfo();
  },

  async getUserApiInfo() {
    var that = this;
    var data = {"name":wx.getStorageSync('uid')}
    const userInfoRes = await WXAPI.getUserInfo(data)
    console.log("获取用户信息成功:"+JSON.stringify(userInfoRes))
    if (userInfoRes.code == 20000) {
      console.log("获取用户信息成功:"+JSON.stringify(userInfoRes.data))
      that.setData({
        apiUserInfoMap: userInfoRes.data
      })
      
    }
    wx.setStorageSync('userid', userInfoRes.data.userId)
    wx.setStorageSync('usertype', userInfoRes.data.userType)
    console.log("用户Id:"+wx.getStorageSync('userid'))
  },
  
  relogin: function () {
    this.setData({
      wxlogin: false
    })
  },
  // 跳转到充值界面
  recharge: function () {
    wx.navigateTo({
      url: "/pages/recharge/index"
    })
  },
  // 跳转到商户界面
  seller: function () {
    wx.navigateTo({
      url: "/pages/seller/index"
    })
  },
  withdraw: function () {
    wx.navigateTo({
      url: "/pages/withdraw/index"
    })
  },
  cancelLogin() {
    this.setData({
      wxlogin: true
    })
  },
  // 获取用户信息
  processLogin(e) {
    console.log("用户信息："+JSON.stringify(e.detail.rawData))
    if (!e.detail.userInfo) {
      wx.showToast({
        title: '已取消',
        icon: 'none',
      })
      return;
    }
    AUTH.login(this,e.detail.rawData);
  },
  scanOrder(){
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        wx.navigateTo({
          url: '/pages/order-details/scan-result?id=' + res.result,
        })
      },
      fail(err){
        console.error(err)
        wx.showToast({
          title: err.errMsg,
          icon: 'none'
        })
      }
    })
  }
})